package com.test.hotel;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.hotel.test.model.Hotel;
import com.hotel.test.model.Local;
import com.hotel.test.model.RomType;
import com.hotel.test.model.impl.HotelImpl;
import com.hotel.test.model.impl.LocalImpl;
import com.hotel.test.model.impl.RomTypeImpl;
import com.hotel.test.service.HotelLocalServiceUtil;
import com.hotel.test.service.LocalLocalServiceUtil;
import com.hotel.test.service.RomTypeLocalServiceUtil;
import com.hotel.test.service.persistence.HotelUtil;
import com.hotel.test.service.persistence.LocalUtil;
import com.hotel.test.service.persistence.RomTypeUtil;
import com.liferay.counter.service.CounterLocalService;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class Admin
 */
public class Admin extends MVCPortlet {
	public void switchAction(ActionRequest request, ActionResponse response)
			throws IOException, PortletException {
		String s = ParamUtil.getString(request, "switchAction");
		String path = "";

		if ("add Local".equalsIgnoreCase(s))
			path = "/html/admin/addLocal.jsp";
		else if ("list Hotel".equalsIgnoreCase(s))
			path = "/html/admin/list_hotel.jsp";
		else if ("add Hotel".equalsIgnoreCase(s))
		
			path = "/html/admin/add_hotel.jsp";
		
		else if ("add RoomType".equalsIgnoreCase(s))
			path = "/html/admin/addRoomType.jsp";
		else if ("Add Category".equalsIgnoreCase(s))
			path = "/admin/category/addCategory.jsp";
		else if ("Review Category".equalsIgnoreCase(s))
			path = "/admin/category/reviewCategory.jsp";

		StringBuffer sbuff = new StringBuffer(ParamUtil.getString(request,"redirectTo"));
		addToPath(sbuff, "path", path);
		//System.out.println(sbuff.toString());
		response.sendRedirect(sbuff.toString());
	}
	
	public void addToPath(StringBuffer sbuff, String name, String value)
			throws IOException {
		
		
			sbuff.append("&" + name + "=");
			sbuff.append(value);
		
	}
	
	

	public void removeExtraPath(RenderRequest request, String path) {
		System.out.println("nhay toi ham remove Path");
		String url = PortalUtil.getCurrentURL(request);
		url = url.substring(0, url.indexOf("&path="));

		request.setAttribute("url", url.toString());
		//System.out.println(url);
	}
	
	public void addLocal(ActionRequest request, ActionResponse response) throws SystemException, IOException
	{
		String localName = ParamUtil.getString(request, "localName");
		String introduce = ParamUtil.getString(request, "introduce");
		String image = ParamUtil.getString(request, "image");
		
		ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
		long userId = themeDisplay.getUserId();
		Local local = new LocalImpl();
		local.setLocalName(localName);
		local.setIntroduce(introduce);
		local.setImage(image);
		local.setUserId(userId);
		LocalLocalServiceUtil.addLocal(local);
		//System.out.println("redirectTo = " +ParamUtil.getString(request, "redirectTo"));
		response.sendRedirect(ParamUtil.getString(request, "redirectTo"));
		
	}
	
	public void editLocal(ActionRequest request, ActionResponse response) throws PortalException, SystemException, IOException, PortletException
	{
		long localId = ParamUtil.getLong(request, "resourcePrimKey");
		//System.out.println("localId = " + localId);
		//Local local = LocalLocalServiceUtil.getLocal(localId);
		//System.out.println("local name : = " +local.getLocalName());
		//System.out.println(localId);
		request.setAttribute("localId", String.valueOf(localId));
		String path = "/html/admin/edit_local.jsp";
		StringBuffer sbuff = new StringBuffer(ParamUtil.getString(request,"redirectTo"));
		//System.out.println("trong ham edit_local = "+sbuff.toString());
		//System.out.println(sbuff.toString());
		addToPath(sbuff, "path", path);
		addToPath(sbuff, "localId", String.valueOf(localId));
		response.sendRedirect(sbuff.toString());
		//storePath(request, path);
	}
	
	
	
	
	public void updateLocal(ActionRequest request, ActionResponse response) throws IOException, PortletException, PortalException, SystemException
	{
		long localId = Long.parseLong(ParamUtil.getString(request, "localId"));
		Local local = LocalLocalServiceUtil.getLocal(localId);
		local.setLocalName(ParamUtil.getString(request, "localName"));
		local.setIntroduce(ParamUtil.getString(request, "introduce"));
		local.setImage(ParamUtil.getString(request, "image"));
		local.setModifiedDate(new  Date());
		LocalLocalServiceUtil.updateLocal(local);
		StringBuffer sbuff = new StringBuffer(ParamUtil.getString(request,"redirectTo"));
		//System.out.println("updateLocal url = " + sbuff.toString());
		addToPath(sbuff, "path", "/html/admin/view.jsp");
		response.sendRedirect(sbuff.toString());
		//storePath(request, path);
		
	}
	
	
	
	public void deleteLocal(ActionRequest request, ActionResponse respone) throws PortalException, SystemException, IOException, PortletException 
	{
		
		long localId = (ParamUtil.getLong(request, "resourcePrimKey"));
		LocalLocalServiceUtil.deleteLocal(localId);
		StringBuffer sbuff = new StringBuffer(ParamUtil.getString(request,"redirectTo"));
		addToPath(sbuff, "path", "/html/admin/view.jsp");
		respone.sendRedirect(sbuff.toString());
		
		
		
	}
	
	public void  getHotels(ActionRequest request, ActionResponse respone) 
			throws NumberFormatException, SystemException, IOException, PortletException
	{
		String localId = request.getParameter("localId");
		//System.out.println("id cua Local /admim/listHOtel: "+ localId);
		StringBuffer sbuff = new StringBuffer(ParamUtil.getString(request,"redirectTo"));
		addToPath(sbuff, "path", "/html/admin/list_hotel.jsp");
		addToPath(sbuff, "localId", localId);
		respone.sendRedirect(sbuff.toString());
		//storePath(request, path);
	}
	
	
	
	
	
	
	
	public void addHotel(ActionRequest request, ActionResponse respone) throws SystemException, IOException, PortletException
	{
		ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
		long userId = themeDisplay.getUserId();
		
		
		Hotel hotel = new HotelImpl();
		
		String hotelName = ParamUtil.getString(request, "hotelName");
		String localId = ParamUtil.getString(request, "localId");
		String quality = ParamUtil.getString(request, "quality");
		
		//UploadPortletRequest req = PortalUtil .getUploadPortletRequest(request);
		

		//String introduce =  ParamUtil.getString(req, "content");
		//UploadPortletRequest uploadReq = PortalUtil.getUploadPortletRequest((ActionRequest)request);
		
		//System.out.println("Day la introduce :"+introduce);
		

		//String introduce =  ParamUtil.getString(req, "content");
		UploadPortletRequest uploadReq = PortalUtil.getUploadPortletRequest((ActionRequest)request);
		String introduce = ParamUtil.getString(request, "introduce");
	//	System.out.println("Day la introduce :"+introduce);
		//System.out.println("local Id :" + local);

		String image = ParamUtil.getString(request, "image");
		String address = ParamUtil.getString(request, "address");
		String email = ParamUtil.getString(request, "email");
		String phone = ParamUtil.getString(request, "phone");
		
		hotel.setHotelName(hotelName);
		hotel.setLocalId(Long.parseLong(localId));
		hotel.setUserId(userId);
		hotel.setQuality(quality);
		hotel.setIntroduce(introduce);
		hotel.setImage(image);
		hotel.setAddress(address);
		hotel.setEmail(email);
		hotel.setPhone(phone);
		
		
		HotelLocalServiceUtil.addHotel(hotel);
		StringBuffer sbuff = new StringBuffer(ParamUtil.getString(request,"redirectTo"));
		//System.out.println("admin add hotel " +sbuff.toString());
		addToPath(sbuff, "path", "/html/admin/list_hotel.jsp");
		addToPath(sbuff, "localId", localId);
		respone.sendRedirect(sbuff.toString());
		
		
	}
	public void editHotel(ActionRequest request, ActionResponse respone) throws PortalException, SystemException, IOException, PortletException
	{
		
		long hotelId = ParamUtil.getLong(request, "resourcePrimKey");
		Hotel hotel = HotelLocalServiceUtil.getHotel(hotelId);
		
		
		request.setAttribute("hotel", hotel);
		String path = "/html/admin/edit_hotel.jsp";
		//storePath(request, path);
		
	}
	
	public void deleteHotel(ActionRequest request, ActionResponse respone) throws PortalException, SystemException
	{
		long hotelId =ParamUtil.getLong(request, "resourcePrimKey");
		HotelLocalServiceUtil.deleteHotel(hotelId);
		
	}
	

	public void updateHotel(ActionRequest request, ActionResponse response) throws IOException, PortletException, PortalException, SystemException
	{
		
		long hotelId =Long.valueOf( ParamUtil.getString(request, "hotelId"));
		Hotel hotel = HotelLocalServiceUtil.getHotel(hotelId);
		
		String hotelName = ParamUtil.getString(request, "hotelName");
		String localId = ParamUtil.getString(request, "localId");
		String quality = ParamUtil.getString(request, "quality");
		String introduce = ParamUtil.getString(request, "introduce");
		String image = ParamUtil.getString(request, "image");
		String address = ParamUtil.getString(request, "address");
		String email = ParamUtil.getString(request, "email");
		String phone = ParamUtil.getString(request, "phone");
		
		hotel.setHotelName(hotelName);
		hotel.setLocalId(Long.parseLong(localId));
		
		hotel.setQuality(quality);
		hotel.setIntroduce(introduce);
		hotel.setImage(image);
		hotel.setAddress(address);
		hotel.setEmail(email);
		hotel.setPhone(phone);
		hotel.setModifiedDate(new Date());
		
		HotelLocalServiceUtil.updateHotel(hotel);
		//String path = "/html/admin/list_hotel.jsp";
		//storePath(request, path);
		
	}
	public void hotelDetail(ActionRequest request, ActionResponse response) throws IOException
	{
		String hotelId = request.getParameter("hotelId");
		StringBuffer sbuff = new StringBuffer(ParamUtil.getString(request,"redirectTo"));
		addToPath(sbuff, "path", "/html/admin/hotel_detail.jsp");
		addToPath(sbuff, "hotelId", hotelId);
		response.sendRedirect(sbuff.toString());
	}
	

	public void addRoomType(ActionRequest request, ActionResponse response) throws IOException, SystemException
	{
		String roomTypeName = ParamUtil.getString(request, "roomTypeName");
		long hotelId = ParamUtil.getLong(request, "hotelId");
		String information = ParamUtil.getString(request, "information");
		int numberOfGuest = ParamUtil.getInteger(request, "numberOfGuest");
		int numberOfRoom = ParamUtil.getInteger(request, "numberOfRoom");
		
		String rice = ParamUtil.getString(request, "rice");
		//long romId = CounterLocalServiceUtil.increment();
		RomType room = new RomTypeImpl();
		room.setHotelId(hotelId);
		room.setRoomName(roomTypeName);
		room.setInformation(information);
		room.setNumberOfGuest(numberOfGuest);
		room.setNumberOfRoom(numberOfRoom);
		room.setRice(rice);
		
		RomTypeLocalServiceUtil.addRomType(room);
		StringBuffer sbuff = new StringBuffer(ParamUtil.getString(request,"redirectTo"));
		//System.out.println("admin add hotel " +sbuff.toString());
		addToPath(sbuff, "path", "/html/admin/list_hotel.jsp");  
		//addToPath(sbuff, "localId", localId);
		response.sendRedirect(sbuff.toString());
		 
	}
	
	
	
	

	//dung lai
	//de ta xem thu
	@Override
	public void doView(RenderRequest request, RenderResponse response) {
		String url = PortalUtil.getCurrentURL(request);
		String path = request.getParameter("path");
		//System.out.println("trong doView: path = "+ path+"url = " + url);
		
		if (path == null)
			path = "/html/admin/view.jsp";
		
		else
			removeExtraPath(request, path);
		System.out.println("trong doView path = " + path);
		try {
			include(path, request, response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PortletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	


}
